from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_tenantEdit

class TenantEdit(QDialog, Ui_tenantEdit.Ui_tenantEditDlg):
    
  StyleSheet = """
QLineEdit[mandatory="true"] {
    background-color: rgb(255, 255, 127);
    color: darkblue;
}
"""

  def __init__(self, dbConn, tipe,  dataGlob, parent=None):
        super(TenantEdit, self).__init__(parent)
        self.setupUi(self)
        self.db = dbConn
        self.tipe = tipe
        self.dataGlob = dataGlob
        
        self.mastModel = QSqlTableModel(self)
        self.mastModel.setTable("master_pemilik_penyewa")
        self.mastModel.setEditStrategy(QSqlTableModel.OnManualSubmit)
        
        self.lineedits = (self.name_le, self.addr_le)
        for lineEdit in self.lineedits:
            lineEdit.setProperty("mandatory", QVariant(True))
        self.setStyleSheet(TenantEdit.StyleSheet)
        
        self.tipeModel = QSqlTableModel(self)
        self.tipeModel.setTable("master_type_tenant")
        self.tipeModel.select()
        self.tenantType_cmb.setModel(self.tipeModel)
        self.tenantType_cmb.setModelColumn(1)
        self.tenantType_cmb.setCurrentIndex(1)
        self.initData()
        
#        untuk ambil data dari cmb
#        brs = self.tenantType_cmb.currentIndex()
#        print self.tipeModel.data(self.tipeModel.index(brs, 0)).toString()
#        print self.tipeModel.data(self.tipeModel.index(brs, 1)).toString()
        
  @pyqtSignature("")
  def on_save_pb_clicked(self):
      if self.tipe == "add":
          if self.tambahData():
              self.close()
          else:
              QMessageBox.warning(self, "Tenant/Owner","Can not add new data.")
      else:
          if self.updateData():
              self.close()
          else:
              QMessageBox.warning(self, "Tenant/Owner","Can not update data")
      
  @pyqtSignature("")
  def on_cancel_pb_clicked(self):
      self.close()

  def tambahData(self):
      if not self.validasi():
          return
      self.mastModel.select()      
      QSqlDatabase.database().transaction()
      if not self.mastModel.insertRow(0):
          return
      self.mastModel.setData(self.mastModel.index(0, 0), QVariant(self.id_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 1), QVariant(self.name_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 2), QVariant(self.addr_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 3), QVariant(self.city_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 4), QVariant(self.prov_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 5), QVariant(self.post_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 6), QVariant(self.phone_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 7), QVariant(self.fax_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 8), QVariant(self.hp_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 9), QVariant(self.comp_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 10), QVariant(self.addr2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 11), QVariant(self.city2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 12), QVariant(self.phone2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 13), QVariant(self.fax2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 14), QVariant(self.email_le.text()))
      brs = self.tenantType_cmb.currentIndex()
      tipeTenant = self.tipeModel.data(self.tipeModel.index(brs, 0)).toString()
      self.mastModel.setData(self.mastModel.index(0, 15), QVariant(tipeTenant))
      if not self.mastModel.submitAll():
          return
      if not QSqlDatabase.database().commit():
          return
      return True

  def updateData(self):
      if not self.validasi():
          return
      self.mastModel.setFilter("kode_pemilik_penyewa = '"+self.tipe+"'")
      self.mastModel.select()
      QSqlDatabase.database().transaction()
      self.mastModel.setData(self.mastModel.index(0, 0), QVariant(self.id_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 1), QVariant(self.name_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 2), QVariant(self.addr_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 3), QVariant(self.city_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 4), QVariant(self.prov_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 5), QVariant(self.post_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 6), QVariant(self.phone_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 7), QVariant(self.fax_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 8), QVariant(self.hp_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 9), QVariant(self.comp_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 10), QVariant(self.addr2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 11), QVariant(self.city2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 12), QVariant(self.phone2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 13), QVariant(self.fax2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 14), QVariant(self.email_le.text()))
      brs = self.tenantType_cmb.currentIndex()
      tipeTenant = self.tipeModel.data(self.tipeModel.index(brs, 0)).toString()
      self.mastModel.setData(self.mastModel.index(0, 15), QVariant(tipeTenant))
      if not self.mastModel.submitAll():
          return
      if not QSqlDatabase.database().commit():
          return
      return True

  def initData(self):
      if self.tipe != "add":
          query = QSqlQuery(self.db)
          qStr = "select kode_pemilik_penyewa, nama_pemilik_penyewa, alamat_pemilik_penyewa, kota_pemilik_penyewa, provinsi_pemilik_penyewa, kode_pos, no_telp, no_fax, no_ponsel, nama_pt, alamat_pt, kota_pt, no_telp_pt, no_fax_pt, email, kode_tt, id_no, npwp from master_pemilik_penyewa where kode_pemilik_penyewa = '"+self.tipe+"'"
          query.exec_(qStr)
          query.next()
          self.id_le.setText(unicode(query.value(0).toString()))
          self.name_le.setText(unicode(query.value(1).toString()))
          self.addr_le.setText(unicode(query.value(2).toString()))
          self.city_le.setText(unicode(query.value(3).toString()))
          self.prov_le.setText(unicode(query.value(4).toString()))
          self.post_le.setText(unicode(query.value(5).toString()))
          self.phone_le.setText(unicode(query.value(6).toString()))
          self.fax_le.setText(unicode(query.value(7).toString()))
          self.hp_le.setText(unicode(query.value(8).toString()))
          self.comp_le.setText(unicode(query.value(9).toString()))
          self.addr2_le.setText(unicode(query.value(10).toString()))
          self.city2_le.setText(unicode(query.value(11).toString()))
          self.phone2_le.setText(unicode(query.value(12).toString()))
          self.fax2_le.setText(unicode(query.value(13).toString()))
          self.email_le.setText(unicode(query.value(14).toString()))
          brs = self.tipeModel.match(self.tipeModel.index(0, 0), 0, query.value(15),1, Qt.MatchWrap )
          self.tenantType_cmb.setCurrentIndex(brs[0].row())
      
  def validasi(self):
      valid = True
      for lineEdit in self.lineedits:
           if lineEdit.text().isEmpty():
               QMessageBox.warning(self, "Tenant / Owner","Please cek mandatory column (Yellow background)")
               return False
      return valid

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = TenantEdit()
    form.show()
    app.exec_()
